<template>
	<view class="login-container">
		<div class="ding"></div>
		<div class="dtitle">
			<p>你好，欢迎登录</p>
		</div>
		<div class="dtu">

			<image src="../../static/images/Group 788.png" mode=""></image>
			<p>商家</p>
		</div>

		<form class="form">
			<view class="form-group">

				<input v-model="username" type="text" placeholder="请输入用户名" class="username" />
			</view>
			<view class="form-group">

				<input v-model="password" type="password" placeholder="请输入密码" class="password" />
			</view>
			<view class="form-group">

				<button type="button" @click="login"
					:class="{ 'btn1': !isNextButtonRed, 'btn2': isNextButtonRed }">登录</button>
				<!-- <button type="button" @click="zhuce">立即注册</button> -->
			</view>
		</form>
		<div class="mi">
			<p @click="zhuce">立即注册</p>|
			<p @click="wang">忘记密码?</p>
		</div>
	</view>
</template>

<script>
	import {
		request,showToast
	} from "../../utils/api.js"
	export default {
		data() {
			return {
				username: '',
				password: ''
			};
		},
		computed: {
			// 计算属性：检查username和yan是否都有值
			isNextButtonRed() {
				return this.username !== '' && this.password !== '';
			}
		},
		methods: {
			zhuce() {

				uni.navigateTo({
					url: '/pages/register/register'
				});
			},
			wang() {

				uni.navigateTo({
					url: '/pages/verification/verification'
				});
			},
			login() {

				const acc = {
					"account": this.username,
					"aliUserId": "",
					"lat": "0",
					"lon": "0",
					"networkType": "0",
					"password": this.password,
					"type": "21"
				}
		const phoneNumberRegex = /^1[3456789]\d{9}$/;
		
		if (phoneNumberRegex.test(this.username)) {
				request('/app/user/login.do', 'POST', acc).then((res) => {
			
					if(res.code==-1){
						return showToast(res.msg)
					}
					if (res.code == 1) {
						uni.switchTab({
							url: '/pages/home/home'
						});
					}
				}).catch((err) => {
					uni.showModal({
						title: '提示',
						content: ` ${err}`,
						showCancel: false, // 是否显示取消按钮
						confirmText: '错误啦111111', // 确认按钮的文字
						success(res) {
							if (res.confirm) {
								console.log('用户点击了确定按钮');
							}
						}
					});
				})
		} else {
		  console.log("不是有效的手机号");
			showToast("不是有效的手机号"); // 中止后续操作并抛出错误
		}
		


		



			}
		}
	};
</script>
<style lang="scss" scoped>
	@import "index.scss"
</style>
